#/bin/bash
##配置各种基于yum管理器的源
mkdir -p /etc/yum.repos.d/
#源站选择
source_yum_mirrors(){
	#1参传入新的镜像
	#2参传入默认镜像站
	#3参传入文件名
	if [[ -n "$1" ]];then
		sed -i "s#$2#$1#g" /etc/yum.repos.d/$3
		unset URL
	fi
}

##源缓存建立
makecache(){
	yum clean all 
	yum makecache
	chek_status "$?" "$1"
	service_install 'yum-utils device-mapper-persistent-data lvm2' > /dev/null
}
#源配置文件备份检测
source_yum_bak(){
	if [[ -d "/yum_bak/" ]];then
		green_zt "源文件已备份"
		rm -f /etc/yum.repos.d/*
	else
		green_zt "正在备份源文件"
		mkdir -p /yum_bak/
		mv /etc/yum.repos.d/* /yum_bak/
		red_z_green_z_info "源备份" "[SCUUESS]"
	fi
}
##openEuler系统源配置
source_yum_openEuler20.03(){
echo '[openEuler-source]
name=openEuler-source
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/source/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/source/RPM-GPG-KEY-openEuler

[openEuler-os]
name=openEuler-os
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/OS/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/OS/x86_64/RPM-GPG-KEY-openEuler

[openEuler-everything]
name=openEuler-everything
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/everything/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/everything/x86_64/RPM-GPG-KEY-openEuler

[openEuler-EPOL]
name=openEuler-epol
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/EPOL/x86_64/
enabled=1
gpgcheck=0
' > /etc/yum.repos.d/openEulerOS.repo 
}
source_yum_openEuler20.09(){
echo '[openEuler-source]
name=openEuler-source
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/source/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/source/RPM-GPG-KEY-openEuler

[openEuler-os]
name=openEuler-os
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/OS/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/OS/x86_64/RPM-GPG-KEY-openEuler

[openEuler-everything]
name=openEuler-everything
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/everything/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/everything/x86_64/RPM-GPG-KEY-openEuler

[openEuler-EPOL]
name=openEuler-epol
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/EPOL/x86_64/
enabled=1
gpgcheck=0
' > /etc/yum.repos.d/openEulerOS.repo 
}
#已确定可用(CentOS7-Base源)
source_yum_centos7(){
echo '[base]
name=CentOS-$releasever - Base
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/os/$basearch/
gpgcheck=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/updates/$basearch/
gpgcheck=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras 
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/extras/$basearch/
gpgcheck=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7' > /etc/yum.repos.d/CentOS-Base.repo
	chek_status "$?" "centos7系统源配置文件写入"
	##判断是否通过1参传入镜像站替换
	source_yum_mirrors "$mirrors_os" "https://mirrors.huaweicloud.com" "CentOS-Base.repo"
	makecache "centos7系统源配置"

}
source_yum_centos8(){
echo '
[base]
name=CentOS-$releasever - Base
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/extras/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/centosplus/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official
 
[PowerTools]
name=CentOS-$releasever - PowerTools
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/PowerTools/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official


[AppStream]
name=CentOS-$releasever - AppStream
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/AppStream/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official' > /etc/yum.repos.d/CentOS-Base.repo
	chek_status "$?" "centos8系统源配置文件写入"
	##判断是否通过1参传入镜像站替换
	makecache "系统源配置"
}
#已确定可用(docker-ce源)
source_yum_docker-ce(){
echo '[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.huaweicloud.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/docker-ce/linux/centos/gpg
' >  /etc/yum.repos.d/Docker.repo
	##判断是否通过1参传入镜像站替换
	source_yum_mirrors "$mirrors_docker"  "https://mirrors.huaweicloud.com" "Docker.repo"
	if [[ "$System_Type" == 'openEuler' ]];then
		green_zt "使用默认Docker版本"
	else
		sed -i "s#/7/#/${System_version}/#g" /etc/yum.repos.d/Docker.repo
	fi
	makecache "Dcoker源配置"
}
##已确定可用(K8S源)
source_yum_k8s(){
echo '[kubernetes]
name=Kubernetes
baseurl=https://mirrors.cloud.tencent.com/kubernetes/yum/repos/kubernetes-el7-$basearch
enabled=1
gpgcheck=0
repo_gpgcheck=0' > /etc/yum.repos.d/kubernetes.repo
	source_yum_mirrors "$URL" "https://mirrors.cloud.tencent.com" "kubernetes.repo"
	makecache "kubernetes源索引建立"
}
#已确定可用（epel7源）
source_yum_epel7(){
cat <<EOF > /etc/yum.repos.d/epel7.repo
[epel7-aliyun]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.ustc.edu.cn/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
 
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=https://mirrors.ustc.edu.cn/epel/7/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
 
[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=https://mirrors.ustc.edu.cn/epel/7/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
EOF
	chek_status "$?" "epel7源配置写入"
	##判断是否通过1参传入镜像站替换
	source_yum-mirrors "https://mirrors.ustc.edu.cn" "epel7.repo"
	makecache  "epel7源获取"
}
#已确定可用
source_yum_epel7-testing(){
cat <<EOF > /etc/yum.repos.d/epel-testing.repo
[epel-testing]
name=Extra Packages for Enterprise Linux 7 - Testing - $basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-epel7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-testing-debuginfo]
name=Extra Packages for Enterprise Linux 7 - Testing - $basearch - Debug
metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-debug-epel7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

[epel-testing-source]
name=Extra Packages for Enterprise Linux 7 - Testing - $basearch - Source
metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-source-epel7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1
EOF
	makecache
	chek_status "$?" "清华epel7源配置"
}
##待确定语法
source_yum-epel8(){
	yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
	sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
	sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
	makecache
	chek_status "$?" "epel8源获取"
}
source_yum_openstack_queens(){
	echo '[centos-openstack-queens]
name=CentOS-7 - OpenStack queens
baseurl=http://mirrors.tuna.tsinghua.edu.cn/$contentdir/$releasever/cloud/$basearch/openstack-queens/
gpgcheck=0
enabled=1' > /etc/yum.repos.d/tsinghua-OpenStack-queens.repo
}